package friendManager;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import loginRegister.LoginBean;
import Bean.CheckBean;
import Bean.SqlBean;

/**
 *
 * @author 刘杉
 */
public class UpdateFriendServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws  IOException {
        String name=request.getParameter("userName");
        String newName=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
        String phone=new String(request.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
        String email=new String(request.getParameter("email").getBytes("ISO-8859-1"),"UTF-8");
        String workPlace=new String(request.getParameter("workPlace").getBytes("ISO-8859-1"),"UTF-8");
        String place=new String(request.getParameter("place").getBytes("ISO-8859-1"),"UTF-8");
        String QQ=new String(request.getParameter("QQ").getBytes("ISO-8859-1"),"UTF-8");
        String userName="";
        HttpSession session=request.getSession();
        LoginBean nn=(LoginBean)session.getAttribute("login");
        if(nn==null){
            response.sendRedirect("http://localhost:8080/login.jsp");
        }
        else if(!CheckBean.isNumeric(phone)){
            CheckBean.alert("联系电话必须为数字！");
            response.sendRedirect("http://localhost:8080/friendManager/updateFriend.jsp");
        }else if(!CheckBean.is_chineseOrAlpha(newName)){
            CheckBean.alert("姓名由汉字或字母组成!");
            response.sendRedirect("http://localhost:8080/friendManager/updateFriend.jsp");
        }else  if(!CheckBean.isNumeric(QQ)){
            CheckBean.alert("QQ号必须为数字！");
            response.sendRedirect("http://localhost:8080/friendManager/updateFriend.jsp");
        }else{
            userName=nn.getUserName();
            String sql="select * from friends where name='"+name+"'and userName='"+userName+"'";
            try {
                ResultSet rs = null;
                SqlBean sb = new SqlBean();
                sb.connectSQL();
                rs = sb.executeQuery(sql);
                rs.last();
                if (rs.getRow() > 0) {
                    CheckBean.alert("用户名已存在，添加失败！");
                    response.sendRedirect("http://localhost:8080/friendManager/updateFriend.jsp");
                } else {
                    String sql1 = "update friends set name='" + newName + "', phone='" + phone + "',email='" + email + "',workPlace='" + workPlace + "',place='" + place + "',QQ='" + QQ + "' where name='" + name + "'and userName='" + userName + "'";
                    sb.executeUpdate(sql1);
                    String sql2 = "select * from friends where userName='" + userName + "'";
                    rs = sb.executeQuery(sql2);
                    ArrayList friendslist = null;
                    friendslist = new ArrayList();
                    while (rs.next()) {
                        FriendBean ff = new FriendBean();
                        ff.setName(rs.getString("name"));
                        ff.setPhone(rs.getString("phone"));
                        ff.setEmail(rs.getString("email"));
                        ff.setWorkPlace(rs.getString("workPlace"));
                        ff.setPlace(rs.getString("place"));
                        ff.setQQ(rs.getString("QQ"));
                        friendslist.add(ff);
                    }
                    session.setAttribute("friendslist", friendslist);
                    sb.closeSQL();
                    CheckBean.alert("填写信息合格，修改成功！");
                    response.sendRedirect("http://localhost:8080/friendManager/updateFriend.jsp");
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws  IOException {
        doGet(request, response);
    }
}